package etc.com.market;


import java.util.ArrayList;

import Controls.JsonDataHelper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;
import etc.com.sql.DBAdapter;
 
public class ListitemActivity extends Activity implements OnItemClickListener{
    /** Called when the activity is first created. */
	private ListView lv;
	private static final int REQUEST_CODE = 10;
	ArrayList<String> arrtext = new ArrayList<String>();
	ArrayList<String> storeadd = new ArrayList<String>();
	
	Bitmap[] idimage;
	int[] tpmImage;
	Bitmap[] idimage1;
	String str; 
	String previous;
	String storename;
	String storeId;
	Cursor cursor;
	String[] productIds;
	
    
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.listitem);
        
        Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String messageReceive = extras.getString("message");
		String storeID = extras.getString("storeId");
		if(storeID != null){
			storeId = storeID;
		}
		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();
			
			previous = str.substring(0, str.indexOf(";"));
			storename = str.substring(str.indexOf(";") + 1);
			
			TextView view = (TextView) findViewById(R.id.storename);
			view.setText(storename);
		}
		
        lv = (ListView)findViewById(R.id.listview);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(this);
        lv.setAdapter(new ImageAdapter(this));
        
		// connect database and get infomation 
		DBAdapter dbApdapter = new DBAdapter(getApplicationContext());
		cursor = dbApdapter.getSpecifyProduct(storeID);
		System.out.println(cursor.getCount());
		int i = 0;
		idimage = new Bitmap[cursor.getCount()];
		idimage1 = new Bitmap[cursor.getCount()];
		productIds = new String[cursor.getCount()];
		tpmImage = new int[cursor.getCount()];
		while(cursor.moveToNext()){
			String id = cursor.getString(0);
			String PRDNM = cursor.getString(1);
			String SUBTITLE = cursor.getString(2);
			String CATEGORY = cursor.getString(3);
			String DSCRPT = cursor.getString(4);
			String VDCLIP = cursor.getString(5);
			String THUMBIMG = cursor.getString(6);
			String UPPRDID = cursor.getString(7);
			String IMAGE = cursor.getString(8);
			
			// set data to item 
			arrtext.add(PRDNM);
			storeadd.add(SUBTITLE);
			String  imageString = "";
			if(THUMBIMG == null || THUMBIMG.equals("")){
				IMAGE = IMAGE.substring(1, IMAGE.length() - 1);
				String[] images = IMAGE.split(",");
				imageString = images[0].trim();
				if(imageString != null){
					if(!imageString.equals("")){
						imageString = imageString.substring(1, imageString.length() - 1);
					}else{
						imageString = "";
					}
				}
			}else{
				imageString = THUMBIMG;
			}
			System.out.println(imageString);
			String imageValue = JsonDataHelper.getImageContent(imageString);
			byte[] decodeBytes = Base64.decode(imageValue.getBytes(), Base64.DEFAULT);
			idimage[i] = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length);
			productIds[i] = id;
			tpmImage[i] = R.drawable.product3;
			i++;
		}
		
		cursor.close();
        
        lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				onClick(view, ("Feature Or Event;"+ storename + ";" + arrtext.get(position) + ";" + idimage[position]), productIds[position]);
			}
		});
        
    }
	

	// function use for footer button ==============================
	public void Previous(View view) {
//		onClickPre(view, (storename));
		cursor.close();
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void Home(View view) {
		cursor.close();
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void SearchProduct(View view) {
		cursor.close();
		onClickSearch(view, "Store Info;" + storename);
	}
	
	public void SpecifyProduct(View view) {
	}
	
	public void Info(View view) {
		cursor.close();
		onClickInfo(view, storename);
	}	
	public void Contact(View view) {
		cursor.close();
			onClickContact(view, storename);
	}
   //===================================================================
	
    public void onClick(View view, String message, String productId) {
    	cursor.close();
		Intent i = new Intent(this, SpecifyProductDetail.class);
		i.putExtra("message", message);
		i.putExtra("ProductId", productId);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);

	}
    
    public void onClickPre(View view, String message) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);

	}
    
    public void onClickSpec(View view, String message) {
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);

	}
	
	public void onClickSearch(View view, String message) {
		Intent i = new Intent(this, SearchOursProducts.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);

	}

	public void onClickInfo(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);

	}
	
	public void onClickContact(View view, String message) {
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
//		Toast.makeText(this, "CHON:"+arg2, Toast.LENGTH_SHORT).show();
//		Intent intent = new Intent(this, SpecifyProduct.class);
//		startActivity(intent);
	}
	
	public class ImageAdapter extends BaseAdapter{
    	private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return arrtext.size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
        	LayoutInflater inflater=LayoutInflater.from(mContext);
        	
    		View row=inflater.inflate(R.layout.row, parent, false);
    		
    		ImageView icon=(ImageView)row.findViewById(R.id.iconfile);    		
    		TextView label=(TextView)row.findViewById(R.id.namefile);
    		ImageView icon1=(ImageView)row.findViewById(R.id.icondetail);
    		icon.setScaleType(ScaleType.FIT_XY);
    		label.setText(arrtext.get(position));  
//    		label1.setText(arrtext.get(position)); 
			icon.setImageBitmap(idimage[position]);
			icon1.setImageBitmap(idimage1[position]);
			
			TextView lbladd = (TextView) row.findViewById(R.id.add);
			lbladd.setText(storeadd.get(position));

    		return row;
        }
    }  
}